"use strict"
var fs = require('fs')
var path = require('path')
var loadDir = require('./loadDir')

module.exports = function(dir) {
  var services = []

  if (!fs.existsSync(dir)) {
    return null
  }
  let files = fs.readdirSync(dir)
  files.forEach(function(filename) {
    let servicePath = path.join(dir, `/${filename}`)
    if (fs.statSync(servicePath).isDirectory()) {
      let modulesPath = path.join(servicePath, '/modules')
      let modules = loadDir(modulesPath)
      services.push({
        type: filename,
        attributes: require(path.join(servicePath, '/attributes.js')),

        servicePath: servicePath,
        modulesPath: modulesPath,
        
        modules: modules
      })
    }
  })
  
  return services
}